package com.admin.action.login_verify_action;

import com.admin.Action;
import com.admin.dao.adminlogindao.AdminDaoImpl;
import com.admin.dao.adminlogindao.AdminDaoInf;
import com.admin.service.login_verify_service.LoginVerifyService;
import com.admin.service.login_verify_service.LoginVerifyServiceImpl;
import com.mysql.cj.Session;
import com.pojo.AdminBean;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import static com.utils.EncryptUtils.desEncrypt;

/**
 * 管理员登录验证
 */

public class LoginVerifyAction extends Action {
    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out=response.getWriter();
        HttpSession session = request.getSession();

        String captcha = request.getParameter("captcha");

        String code =(String) session.getAttribute("code");
//        System.out.println(code+"111"+captcha);
        session.removeAttribute("code");
        if (!code.equalsIgnoreCase(captcha)){
            out.print("验证码错误");
//            request.setAttribute("login_msg","验证码错误");
//            request.getRequestDispatcher("/adminLogin.html").forward(request,response);
            return;
        }


        String username = request.getParameter("username");
        String password = request.getParameter("password");

        try {
            //编码
            password=desEncrypt(password);
        } catch (Exception e) {
            e.printStackTrace();
        }

        AdminBean ab=new AdminBean();
        ab.setAdminName(username);
        ab.setAdminPwd(password);


//        System.out.println(username);
//        System.out.println(password);
        LoginVerifyServiceImpl loginVerifyService = new LoginVerifyServiceImpl();
        Boolean  loginCheck=  loginVerifyService.adminCheck(ab);//得到用户的信息
        System.out.println("是否正确："+loginCheck);

        if (loginCheck){
           AdminBean adminAllInfo= loginVerifyService.getAdminAllInfo(username);
           HttpSession sessionAdmin= request.getSession();
            sessionAdmin.setAttribute("adminInfo", adminAllInfo);
            System.out.println(adminAllInfo);
            request.getRequestDispatcher("/library-admin-view/index.html").forward(request,response);
        }else {
            out.print("用户名或密码错误");
        }



    }
}
